#include <conio.h>
#include "GRAFIKA.H"
#include "INPUT.H"

#define FPS 30
#define DELTA 1000/FPS

#define NUM_BUTTONS         8
#define STATE_ACTIVE        0
#define STATE_NORM        	1

#define CANVAS_WIDTH 1024
#define CANVAS_HEIGHT 768

#define SCROLL_WIDTH 295
#define SCROLL_HEIGHT 143

int main()
{	
	int CLIP_WIDTH = 253, CLIP_HEIGHT = 157;
	int i=0,j=0,y=0,x=0;
	int tInput;	
	MouseState prevmouseState;
	MouseState currentmouseState;
	// Posisi scroll
	int posscrollx = 8;
	int posscrolly = 40;
	int scrolllenx,scrollleny;
	int buttonSize;
	boolean mouseEnable;
	byte numButtons;
	BITMAP bmp;
	BUTTON *button[NUM_BUTTONS];
	boolean valid;
	byte data;
	setMode(VGA_MODE);	
	mouseEnable = initMouse(&numButtons);
	if(mouseEnable){
		prevmouseState = getMouseState();
		showMouseCursor();
	}
	for (i=0; i<NUM_BUTTONS; i++)
	{
		if ((button[i] = (BUTTON *) malloc(sizeof(BUTTON))) == NULL)
		{
		  printf("Error allocating memory for bitmap.\n");
		  exit(EXIT_FAILURE);
		}
		else
		{			
			button[i]->x = 0;
			button[i]->y = (16*i) +32;
			button[i]->state = STATE_NORM;
			button[i]->height = 16;
			button[i]->width = 16;
			button[i]->numState = 2;
			button[i]->bitmap = (byte*)(malloc(sizeof(byte)*button[i]->height*button[i]->width*button[i]->numState));
		}
	}
	valid = loadBMP("tool.bmp", &bmp);
	for(i=0;i<NUM_BUTTONS;i++){
		buttonSize  = button[i]->width*button[i]->height;
		for(j=0;j<button[i]->numState;j++)	{
			for(y=0;y<button[i]->height;y++){
				for(x=0;x<button[i]->width;x++){
				  button[i]->bitmap[j*buttonSize+x+y*button[i]->width] = bmp.data[i*(button[i]->numState*button[i]->width) + j*button[i]->width + x +y*bmp.width];
				}
			}			
		}		  
	}
	free(bmp.data);                     /* free up memory used */

	while(TRUE)
	{
		
		if(kbhit())
		{
			tInput = getKey();
			if(tInput == 27)
			{
				break;
			}			
		}
		if(mouseEnable){
			currentmouseState = getMouseState();
			for(i=0;i<NUM_BUTTONS;i++){
				button[i]->state=STATE_NORM;
			}
			if(currentmouseState.x < button[0]->width && currentmouseState.x >= 0  && currentmouseState.y>=32 && currentmouseState.y < (NUM_BUTTONS*button[0]->height)+32){
				button[(currentmouseState.y-32)/button[0]->height]->state=STATE_ACTIVE;
			}
			if(currentmouseState.x > posscrollx && currentmouseState.x < posscrollx+scrolllenx && currentmouseState.y > SCREEN_HEIGHT-10 && currentmouseState.y < SCREEN_HEIGHT-2 && (currentmouseState.buttonState&1==1)){ 
				// Klik scroll horizontal
				posscrollx += currentmouseState.x-prevmouseState.x ;
				if (posscrollx<8){
					posscrollx= 8;
				} else if (posscrollx+scrolllenx>SCREEN_WIDTH-17){
					posscrollx=SCREEN_WIDTH-17-scrolllenx;
				}
			}
			if(currentmouseState.y > posscrolly && currentmouseState.y < posscrolly+scrollleny && currentmouseState.x > SCREEN_WIDTH-10 && currentmouseState.x < SCREEN_WIDTH-2 && (currentmouseState.buttonState&1==1)){ 
				// Klik scroll vertical
				posscrolly += currentmouseState.y-prevmouseState.y ;
				if (posscrolly<40){
					posscrolly = 40;
				} else if (posscrolly+scrollleny>SCREEN_HEIGHT-17){
					posscrolly = SCREEN_HEIGHT-17-scrollleny;
				}
			}
			prevmouseState = currentmouseState;
		}
		if (CLIP_WIDTH<CANVAS_WIDTH){
			scrolllenx = (int)((long)CLIP_WIDTH*(long)SCROLL_WIDTH/CANVAS_WIDTH);
		} else {
			scrolllenx = SCROLL_WIDTH;
		}
		if (CLIP_HEIGHT<CANVAS_HEIGHT){
			scrollleny = (int)((long)CLIP_HEIGHT*(long)SCROLL_HEIGHT/CANVAS_HEIGHT);
		} else {
			scrollleny = SCROLL_HEIGHT;
		}
		x=(int)((long)(posscrollx-8)*(long)CANVAS_WIDTH/(long)SCROLL_WIDTH);
		y=(int)((long)(posscrolly-40)*(long)CANVAS_HEIGHT/(long)SCROLL_HEIGHT);
		setBackground(255);
		fillRectangle(0,0,319,31,247);
		drawRectangle(16,32,271,191,247);
		if(valid){
			for(i=0;i<NUM_BUTTONS;i++){
				drawButton(button[i], 255);
			}
		}
		setPalette(bmp.palette);
		x = 272;
		y = 32;
		for(i=0;i<NUM_COLORS;i++){
			fillRectangle(x,y,x+4,y+4,i);
			x+=5;
			if(x>SCREEN_WIDTH-13){
				y+=5;
				x=272;
			}
		}
		// Scroll Horizontal
		fillRectangle(0,SCREEN_HEIGHT-8,7,SCREEN_HEIGHT-1, LIGHT_BLUE);
		fillRectangle(SCREEN_WIDTH-16,SCREEN_HEIGHT-8,SCREEN_WIDTH-9,SCREEN_HEIGHT-1, LIGHT_BLUE);
		fillRectangle(8,SCREEN_HEIGHT-8,SCREEN_WIDTH-17,SCREEN_HEIGHT-1, 240);
		fillRectangle(posscrollx,SCREEN_HEIGHT-8,posscrollx+scrolllenx,SCREEN_HEIGHT-1, 216);
		
		// Scroll Vertikal
		fillRectangle(SCREEN_WIDTH-8,32,SCREEN_WIDTH-1,39, LIGHT_BLUE);
		fillRectangle(SCREEN_WIDTH-8,SCREEN_HEIGHT-16,SCREEN_WIDTH-1,SCREEN_HEIGHT-9, LIGHT_BLUE);
		fillRectangle(SCREEN_WIDTH-8,40,SCREEN_WIDTH-1,SCREEN_HEIGHT-17, 240);
		fillRectangle(SCREEN_WIDTH-8,posscrolly,SCREEN_WIDTH-1,posscrolly+scrollleny, 216);
		redraw();
		delay(DELTA);
	}
	setMode(TEXT_MODE);
	return EXIT_SUCCESS;
}
